package cn.wolfcode;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * 爬虫
 */
public class Dsfa {
    public static void main(String[] args) {
		HttpClient client = new HttpClient();
		GetMethod getMethod = new GetMethod(
				"https://www.shicimingju.com/chaxun/list/4059.html");
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		try {
			int statusCode = client.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				System.out.println("获取失败..." + getMethod.getStatusLine());
			}
			byte[] responseBody = getMethod.getResponseBody();
			/*FileOutputStream fileOutputStream = new FileOutputStream(
					"E:\\chenjiwei\\白居易琵琶行.txt");// 将文件读取到本地文本
			*/
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream("E:\\chenjiwei\\白居易琵琶行.txt")));
			// 处理内容
			String html = new String(responseBody);
			//3. 解析数据:
			Document document = Jsoup.parse(html);

			//获得本章的章节名称 并输出到文本中
			//Elements chapterName = document.select(".j_chapterName");
			//System.out.println(chapterName.text());

			System.out.println("======================================================");

			Element title = document.getElementById("zs_title");
			String zstitle = title.text();
			System.out.println(zstitle);

			Elements zuozhe = document.getElementsByClass("niandai_zuozhe");
			String niandaizuozhe = zuozhe.text();
			System.out.println(niandaizuozhe);

			Element xu = document.getElementById("zs_xu");
			String zsxu = xu.text();
			System.out.println(zsxu);

			Element content = document.getElementById("zs_content");
			String zscontent = content.text();
			System.out.println(zscontent);

			System.out.println("======================================================");

			//指定内容
			bw.write(title.text());
			bw.write(zuozhe.text());
			bw.write(xu.text());
			bw.write(content.text());
			bw.newLine();
			bw.flush();

			//整个页面
			//fileOutputStream.write(responseBody);
			//fileOutputStream.write();
			//fileOutputStream.close();
		} catch (HttpException e) {
			e.printStackTrace();
			System.out.println("获取失败，请重新获取...");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			getMethod.releaseConnection();
		}
	}
}